import tkinter as tk
import os
from log import *
from PIL import Image, ImageTk


def GetFileExtension(file_path)->str:
    '''获取文件的后缀名'''
    _, extension = os.path.splitext(file_path)
    return extension

class ScrollbarWithEntry(tk.Frame):
    '''
    带有输入框和提示标签的滚动条组件
    '''
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.value = tk.IntVar()
        # 创建组件
        self.label = tk.Label(self, text="Label")
        self.entry = tk.Entry(self, textvariable=self.value, width=5, justify=tk.CENTER)
        self.scale = tk.Scale(self, variable=self.value, from_=0, to=100, orient=tk.HORIZONTAL)  # 创建滑块并与self.value建立连接

    def PackInnerWidget(self):
        self.label.pack(side=tk.LEFT)
        self.entry.pack(side=tk.LEFT)
        self.scale.pack(side=tk.RIGHT, fill=tk.X, expand=1)

    def ConfigLabel(self, **kwargs):
        self.label.configure(**kwargs)

    def ConfigEntry(self, **kwargs):
        self.entry.configure(**kwargs)


    def ConfigScale(self, **kwargs):  # 新增一个方法来设置滑块的属性
        self.scale.configure(**kwargs)

class ImageCanvas(tk.Canvas):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.is_moving = False
        self.move_gain = 0.8
        self.last_x = None
        self.last_y = None
        self.x = None
        self.y = None
        self.image_x = 0
        self.image_y = 0
        self.scale_ = 1.0
        self.bind("<ButtonPress-1>", self.start_move)
        self.bind("<B1-Motion>", self.move_image)
        self.bind("<ButtonRelease-1>", self.stop_move)
        self.bind("<MouseWheel>", self.zoom_image)
        self.image = None
        self.image_id = None

    def start_move(self, event):
        self.is_moving = True
        self.last_x = event.x
        self.last_y = event.y
        self.x = event.x
        self.y = event.y

    def move_image(self, event):
        if not self.is_moving:
            return
        dx = int(round(event.x - self.last_x)) * self.move_gain
        dy = int(round(event.y - self.last_y)) * self.move_gain
        self.last_x = self.x
        self.last_y = self.y
        self.x = event.x
        self.y = event.y
        image_coords = self.coords(self.image_id)
        self.image_x = image_coords[0]
        self.image_y = image_coords[1]
        self.move('all', dx, dy)

    def stop_move(self, event):
        self.is_moving = False

    def zoom_image(self, event):
        self.scale_ = self.scale_ * (1.0 + event.delta/1000)
        self.DrawImage()

    def UpdateImage(self, image:Image.Image):
        self.image = image
        self.DrawImage()
    
    def DrawImage(self):
        width, height = self.image.size
        new_width = int(round(width * self.scale_))
        new_height = int(round(height * self.scale_))
        self.show_image = ImageTk.PhotoImage(self.image.resize((new_width, new_height)))
        self.delete('all')
        self.image_id = self.create_image(self.image_x, self.image_y, anchor='nw', image=self.show_image)

